package com.atguigu.admin.controller;

import com.atguigu.admin.bean.City;
import com.atguigu.admin.bean.TbUserInfo;
import com.atguigu.admin.bean.User;
import com.atguigu.admin.service.CityService;
import com.atguigu.admin.service.UserInfoService;
import com.sun.org.glassfish.gmbal.ParameterNames;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
@Slf4j
@Controller
public class IndexController {

    @Autowired
    UserInfoService userInfoService;
    @Autowired
    CityService cityService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @ResponseBody
    @GetMapping("/quickCity")
    public City getCityById(@RequestParam  Long id) {
        return cityService.getCityNameById(id);
    }
    @ResponseBody
    @PostMapping("/saveCity")
    public City saveCity(City city) {
        cityService.save(city);
        return city;
    }
    @ResponseBody
    @GetMapping("/userInfo")
    public TbUserInfo getUserInfo(@RequestParam Long id) {
        return userInfoService.getUserInfo(id);
    }

    @Autowired
    JdbcTemplate jdbcTemplate;
    @ResponseBody
    @GetMapping("/sql")
    public String quyerDb(){
        Long l = jdbcTemplate.queryForObject("select count(1) from user", Long.class);
        return l.toString();
    }
    @GetMapping(value = {"/", "/login", "/login.html"})
    public String loginPage(){
        return "login";
    }

    @PostMapping("/login")
    public String main(User user, HttpSession session, Model model){
        String userName = user.getUserName();
        String password = user.getPassword();
        if(StringUtils.hasLength(userName) && "123456".equals(password)){
            session.setAttribute("loginUser", user);
            //重定向避免重复提交问题
            return "redirect:/main.htm";
        }else{
            model.addAttribute("msg", "账号密码错误，请重新登录");
            return "login";
        }

    }

    @GetMapping("/main.htm")
    public String mainPage(HttpSession session, Model model){
       /* Object loginUser = session.getAttribute("loginUser");
        if(loginUser != null){
            return "main";
        }else{
            model.addAttribute("msg", "账号密码为空，请重新登录");
            return "login";
        }*/
        log.info("mainPage 当前线程：{}", Thread.currentThread().getName());

        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
        String s = operations.get("/main.htm");
        String s1 = operations.get("/dynamic_table");
        model.addAttribute("mainCount", s);
        model.addAttribute("dynamicCount", s1);

        return "main";
    }
}
